package com.soap.desensitize.strategy.irreversible;

import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;

import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;

/**
 * @desc：   手机号的加密
 * @author： QGP
 * @create： 2022/4/27 14:44
 */
public class PhoneStrtegy extends AbstractIRreversibleEncry<String> {

    private static PhoneStrtegy instance = null;

    private PhoneStrtegy(){}

    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized PhoneStrtegy getInstance(){
        if(instance == null){
            instance = new PhoneStrtegy();
        }
        return instance;
    }

    /**
     * @desc 校验是否是手机号
     * @param phoneNumber
     * @return
     */
    public static  boolean checkPhone(String phoneNumber){
        String pattern = "^[1][3,5,7,8]\\d{9}$";
        boolean matches = Pattern.matches(pattern, phoneNumber);
        return matches;
    }

    /**
     * @desc  手机号加密
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String encode(String data) {
        boolean checkPhone = checkPhone(data);
        System.out.println("手机后验证"+checkPhone);
        String encodePhoneNumber= "";
        if (checkPhone){
            encodePhoneNumber  = data.substring(0, 3) + "****"
                    + data.substring(7, data.length());
        }
        return encodePhoneNumber;
    }
}
